/*
 * @(#) Coordinate.java 2013-8-14 上午10:23:34
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.base.control;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import com.datasweep.compatibility.ui.ContentAlignment;
import com.datasweep.compatibility.ui.FlatLabel;
import com.datasweep.compatibility.ui.Panel;
import com.datasweep.plantops.swing.ControlDock;
import com.rockwell.activity.ItemDescriptor;


 /**
  * 均温坐标
  * @author Nemo, 2013-8-14 上午10:23:46
  */
public class Coordinate extends BaseUIActivity
{
	public static final int ARROW_LENGTH = 4;
	
	Panel coodinatePanel;
	Panel coodinateValuePanel;
	FlatLabel labelValue;
	
	int xMax = 100;		//x最大值
	int yMax = 30;		//y最大值
	
	int x = 0;
	int y = 0;
	boolean clicked = false;
	
	public Coordinate()
	{
		super();
		coodinatePanel = createPanel("coodinatePanel", ControlDock.TOP,100,60);
		coodinatePanel.setBackColor(Color.WHITE);
		coodinatePanel.addMouseListener(new MouseListener()
		{
			
			@Override
			public void mouseReleased(MouseEvent e)
			{
			}
			
			@Override
			public void mousePressed(MouseEvent e)
			{
			}
			
			@Override
			public void mouseExited(MouseEvent e)
			{
			}
			
			@Override
			public void mouseEntered(MouseEvent e)
			{
			}
			
			@Override
			public void mouseClicked(MouseEvent e)
			{
				labelValue.setText(x*xMax/100 + "," + y*yMax/60);
				clicked = !clicked;
			}
		});
		
		coodinatePanel.addMouseMotionListener(new MouseMotionListener()
		{
			
			@Override
			public void mouseMoved(MouseEvent e)
			{
				if (!clicked)
				{
					x = e.getX();
					y = e.getY();

					Graphics2D g = (Graphics2D) coodinatePanel.getJComponent().getGraphics();
					coodinatePanel.getJComponent().paint(
						g);
					g.drawLine(
						x, 0, x, 100);
					g.drawLine(
						0, y, 100, y);

					labelValue.setText(x*xMax/100 + "," + y*yMax/60);
				}
			}
			
			@Override
			public void mouseDragged(MouseEvent e)
			{
			}
		});
		add(coodinatePanel);
		
		coodinateValuePanel = createPanel("coodinateValuePanel", ControlDock.FILL);
		labelValue = createFlatLabel("labelValue", null);
		labelValue.setDock(ControlDock.FILL);
		labelValue.setTextAlign(ContentAlignment.MIDDLECENTER);
		labelValue.setText("0.0,0.0");
		coodinateValuePanel.add(labelValue);
		add(coodinateValuePanel);
		
		setPreferredSize(new Dimension(100, 70));
		
	}
	
	@Override
	public ItemDescriptor[] configurationDescriptors()
	{
		return new ItemDescriptor[]{ItemDescriptor.createItemDescriptor(this.getClass(), "xMax", Integer.class, new Object[]{ItemDescriptor.SHORTDESCRIPTION,"X轴最大值"})
			,ItemDescriptor.createItemDescriptor(this.getClass(), "yMax", Integer.class, new Object[]{ItemDescriptor.SHORTDESCRIPTION,"Y轴最大值"})};
	}
	
	@Override
	protected void configurationItemSet(String key, Object value)
	{
		if(key.equals("xMax"))
		{
			this.xMax = (Integer) value;
		}
		else if(key.equals("yMax"))
		{
			this.yMax = (Integer) value;
		}
	}
	
//	/**
//	 * 设置最大值
//	 * @param xValue	X轴
//	 * @param yValue	Y轴
//	 */
//	public void setMaxValue(int xValue,int yValue)
//	{
//		this.xMax = xValue;
//		this.yMax = yValue;
//	}
	
	public String getValue()
	{
		return labelValue.getText();
	}
	
	@Override
	protected void startup()
	{
		super.startup();
		xMax = (Integer) getConfigurationItem("xMax");
		yMax = (Integer) getConfigurationItem("yMax");
	}
}
